package com.example.controller.pc;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.annotation.Log;
import com.example.common.result.Result;
import com.example.entity.mysql.Factory;
import com.example.entity.mysql.Resource;
import com.example.service.pc.FactoryService;
import com.example.service.pc.ResourceService;
import com.example.untils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LiangPandeng
 * @since 2024-05-19
 */
@RestController
@Api(tags = "工厂")
@RequestMapping("/api/factory")
public class FactoryController {

    @Autowired
    private FactoryService factoryService;

    @Autowired
    private ResourceService resourceService;

    @Log("添加工厂")
    @ApiOperation("添加工厂")
    @PostMapping("addFactory")
    public Result addFactory(@RequestBody Factory factory) {
        if(StringUtils.isEmpty(factory.getFactoryno())
                || StringUtils.isEmpty(factory.getFactoryname()))  {
            return Result.error("工厂名或工厂编号为空");
        }
        int count = factoryService.count(new LambdaQueryWrapper<Factory>()
                .eq(Factory::getFactoryno, factory.getFactoryno()));
        if(count > 0) {
           Result.error("工厂编号已被使用");
        }
        factory.setFactoryid(null);
        factoryService.save(factory);
        return Result.success("工厂添加成功");
    }

    @ApiOperation("工厂列表")
    @PostMapping("factoryList")
    public Result<List<Factory>> factoryList() {
        return Result.success(factoryService.list());
    }

    @Log("编辑工厂")
    @ApiOperation("编辑工厂")
    @PostMapping("editFactory")
    public Result editFactory(@RequestBody Factory factory) {
        if(StringUtils.isEmpty(factory.getFactoryno())
                || StringUtils.isEmpty(factory.getFactoryname())
                || factory.getFactoryid() == null)  {
            return Result.error("工厂名或工厂编号或工厂ID为空");
        }
        Factory one = factoryService.getOne(new LambdaQueryWrapper<Factory>()
                .eq(Factory::getFactoryno, factory.getFactoryno()));
        if(one != null && !one.getFactoryid().equals(factory.getFactoryid())) {
            Result.error("工厂编号已被使用");
        }
        factoryService.updateById(factory);
        return Result.success("工厂修改成功");
    }

    @Log("删除工厂")
    @ApiOperation("删除工厂")
    @PostMapping("delFactory")
    public Result delFactory(@RequestBody Integer factoryId) {
        if(factoryId == null)  {
            return Result.error("工厂名或工厂编号或工厂ID为空");
        }
        int count = resourceService.count(new LambdaQueryWrapper<Resource>()
                .eq(Resource::getFactoryId, factoryId));
        if(count > 0) {
            return Result.error("工厂已被资源使用,不能删除");
        }
        boolean b = factoryService.removeById(factoryId);
        if(b) return Result.success("工厂删除成功");
        else return Result.success("工厂删除成功");
    }

}
